<script setup>
import router from '../router';
import { getCurrentInstance, reactive, ref, onMounted } from 'vue'
const { proxy } = getCurrentInstance();
import { useRoute } from "vue-router";
const route = useRoute();
import { fetchApi } from '../apis/fetchapi'
import { userStore } from '../store/index'
const userSData = userStore();

import GoBack from '../components/GoBack.vue'

const activityData = ref({});
const id = route.query.id
onMounted(() => {

  fetchApi('/act-detail', {
    id: id
  }).then(res => {
    if (res.code === 200) {
      console.log(res)
      activityData.value = res.data
    } else {
      proxy.$toast({
        value: res.msg,
        type: 'fail'
      })
    }
  })


})

const recruitSuccess = ref(false);
const btnStatus=ref(false);
function toApply(id) {
  fetchApi('/apply-act', { actId: id }).then(res => {
    if (res.code === 200) {
      recruitSuccess.value = true;
    }else if(res.code===10014){
      proxy.$toast({
        value: res.msg,
      })
      btnStatus.value = true; 
     } else {
      proxy.$toast({
        value: res.msg,
      })
    }
  })
}

</script>

<template>
  <div class="recruit-container">
    <GoBack title="活动详情"></GoBack>
    <div class="recruit">
      <div class="recruit-head">
        <div class="name">{{ activityData.title }}</div>
        <div class="time">活动时间：{{ activityData.beginTime }}-{{ activityData.endTime }}</div>
        <div class="status apply" v-if="activityData.status === 2">已结束</div>
        <div class="status" v-else>招募中</div>
      </div>
      <div class="recruit-con" v-html="activityData.act_rule"></div>
      <div class="recruit-bottom">
        <div class="to-apply" v-if="!btnStatus" @click="toApply(activityData.id)">申请活动</div>
        <div class="to-manage" v-else @click="router.push('/mine')">管理我的活动</div>
      </div>
      <div class="recruit-success" v-show="recruitSuccess">
        <div class="rs">
          <p>申请成功 <br>请进入我的活动留意活动进程， <br>谢谢！</p>
          <div class="close-rs" @click="recruitSuccess = fasle">确认</div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.recruit {
  margin: 10px auto 0;
  padding-bottom: 2px;
  border-radius: 20px 20px 0 0;
  background: rgb(245, 245, 245);
  position: relative;

  &-container {
    width: 100%;
    overflow: auto;
    height: 100vh;
  }

  &-head {
    box-sizing: border-box;
    padding: 25px 18px 0;
    position: relative;
    height: 102px;

    .name {
      height: 30px;
      line-height: 30px;
      text-align: left;
      font-size: 16px;
      width: 100%;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      color: rgb(62, 66, 73);
      margin-bottom: 15px;
    }

    .time {
      font-size: 12px;
      color: rgb(146, 146, 176);
      height: 20px;
      line-height: 20px;
      text-align: left;
    }

    .status {
      width: 72px;
      height: 35px;
      font-size: 12px;
      font-weight: 400;
      color: rgb(251, 251, 251);
      text-align: center;
      line-height: 35px;
      cursor: pointer;
      background: rgb(233, 157, 66);
      position: absolute;
      bottom: 0;
      right: 20px;

      &.apply {
        background: rgb(187, 187, 187);
      }
    }
  }

  &-con {
    height: calc(100vh - 102px - 54px - 160px);
    background: #ffffff;
    width: 100%;
    font-size: 14px;
    box-sizing: border-box;
    padding: 20px;
    overflow: auto;
    color: rgb(16, 16, 16);

    p {
      font-size: 14px;
      color: rgb(16, 16, 16);
    }
  }

  &-bottom {
    height: 85px;
    display: flex;
    align-items: center;
    justify-content: center;

    div {
      width: 140px;
      height: 35px;
      text-align: center;
      line-height: 35px;
      text-align: center;
      font-size: 14px;
      color: #ffffff;
      background: rgb(231, 56, 34);
      border-radius: 18px;
      cursor: pointer;

      &.to-manage {
        background: rgb(233, 157, 66);
      }
    }

  }

  &-success {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    right: 0;
    margin: auto;
    background: rgba(0, 0, 0, 0.6);
    z-index: 2;
    display: flex;
    align-items: center;
    justify-content: center;

    .rs {
      width: 282px;
      height: 229px;
      background: rgb(251, 251, 251);
      text-align: center;
      box-sizing: border-box;
      padding: 28px 0 0;

      p {
        color: rgb(64, 149, 229);
        font-size: 16px;
        font-weight: bold;
        margin-bottom: 42px;
      }

      .close-rs {
        background: rgb(64, 149, 229);
        width: 80px;
        height: 30px;
        line-height: 30px;
        text-align: center;
        margin: 0 auto;
        border-radius: 4px;
        font-size: 14px;
        color: #ffffff;
      }
    }
  }
}
</style>